// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import DelayProduct from "../pages/DelayProduct.vue";
import Supplier from "../pages/Supplier.vue";
// import Warehouse from "../api/warehouse.js";
import Customer from "../pages/Customer.vue";
import Warehouse from "../pages/Warehouse.vue";

// 页面组件
const Login = () => import('../pages/Login.vue')
const MainLayout = () => import('../views/MainLayout.vue') // 新增：主布局

// 其他页面（保持懒加载）
const Dashboard = () => import('../pages/Dashboard.vue')
const Users = () => import('../pages/Users.vue')
const Employee = () => import('../pages/Employee.vue')
const RawMaterial = () => import('../pages/wms/RawMaterial.vue')
const MaterialRequisition = () => import('../pages/wms/MaterialRequisition.vue')
const ProductInput = () => import('../pages/wms/ProductInput.vue')
const ProductShipment = () => import('../pages/wms/ProductShipment.vue')
const ProcurePlan = () => import('../pages/wms/ProcurePlan.vue')
const OrderPlan = () => import('../pages/wms/OrderPlan.vue')
const PayMoney = () => import('../pages/wms/PayMoney.vue')
const CategoryIndex = () => import('../pages/CategoryIndex.vue')
const CategoryAdd = () => import('../pages/CategoryAdd.vue')
const ChildCategory = () => import('../pages/ChildCategory.vue')
const Product = () => import('../pages/Product.vue')
const Department = () => import('../pages/Department.vue')
const UnitIndex = () => import('../pages/UnitIndex.vue')
const Order = () => import('../pages/Order.vue')
const PaymentSuccess = () => import('../pages/PaymentSuccess.vue')
const OrderReturn = () => import('../pages/OrderReturn.vue')

const routes = [
    // ✅ 1. 独立页面：登录
    {
        path: '/login',
        name: 'login',
        component: Login,
        meta: { title: '登录' }
    },
    { path: '/payment/success', name: 'PaymentSuccess', component: PaymentSuccess, meta: { title: '支付成功' } },

    // ✅ 2. 主布局包裹的页面
    {
        path: '/',
        component: MainLayout,
        redirect: '/dashboard',
        children: [
            { path: 'dashboard', name: 'dashboard', component: Dashboard, meta: { title: '数据统计' } },
            { path: 'dashboard/overview', name: 'dashboard-overview', component: Dashboard, meta: { title: '数据统计 · 概览' } },
            { path: 'dashboard/charts', name: 'dashboard-charts', component: Dashboard, meta: { title: '数据统计 · 图表' } },

            { path: 'users', name: 'users', component: Users, meta: { title: '用户管理' } },
            { path: 'users/list', name: 'users-list', component: Users, meta: { title: '用户管理 · 用户列表' } },
            { path: 'users/roles', name: 'users-roles', component: Users, meta: { title: '用户管理 · 角色管理' } },
            { path: 'systemManagement/department', name: 'users-department', component: Department, meta: { title: '用户管理 · 角色管理' } },

            { path: 'employee', name: 'employee', component: Employee, meta: { title: '员工管理' } },
            { path: 'employee/list', name: 'employee-list', component: Employee, meta: { title: '员工管理 · 员工列表' } },

            { path: 'rawMaterial', name: 'rawMaterial', component: RawMaterial, meta: { title: '原材料入库' } },
            { path: 'materialRequisition', name: 'materialRequisition', component: MaterialRequisition, meta: { title: '原材料领料' } },
            { path: 'productInput', name: 'productInput', component: ProductInput, meta: { title: '成品入库' } },
            { path: 'productShipment', name: 'productShipment', component: ProductShipment, meta: { title: '商品发货' } },
            { path: 'procurePlan', name: 'procurePlan', component: ProcurePlan, meta: { title: '采购计划' } },
            { path: 'orderPlan', name: 'orderPlan', component: OrderPlan, meta: { title: '采购订单' } },
            { path: 'payMoney', name: 'payMoney', component: PayMoney, meta: { title: '财务付款' } },

    {path: '/systemManagement',name:'systemManagement',component: Product ,meta: {title: '系统管理'} },
    {path: '/systemManagement/delayProduct',name:'systemManagement-delayProduct',component: DelayProduct ,meta: {title: '临期商品管理'} },
    {path: '/systemManagement/categoryIndex',name:'systemManagement-categoryIndex',component: CategoryIndex ,meta: {title: '商品类别管理'} },
    {path: '/systemManagement/categoryAdd',name:'systemManagement-categoryAdd', component: CategoryAdd ,meta: {title: ' 添加商品主类别'} },
    {path: '/systemManagement/childCategory/:categoryId',name:'systemManagement-childCategory',props:true, component: ChildCategory,meta: {title: ' 查看商品子类别'}},
    {path: '/systemManagement/product',name:'systemManagement-product',component: Product ,meta: {title: '商品资料管理'} },
    {path: '/systemManagement/categoryIndex',name:'systemManagement-categoryIndex',component: CategoryIndex ,meta: {title: '系统管理 · 商品类别管理'} },
    {path: '/systemManagement/categoryAdd',name:'systemManagement-categoryAdd', component: CategoryAdd},
    {path: '/systemManagement/childCategory/:categoryId',name:'systemManagement-childCategory',props:true, component: ChildCategory},
    {path: '/systemManagement/unitIndex',name:'systemManagement-unitIndex', component: UnitIndex,meta: {title: '系统管理 · 区域管理'}},
    {path: '/systemManagement/product',name:'systemManagement-product',component: Product ,meta: {title: '系统管理 · 商品资料管理'} },
    {path: '/systemManagement/supplier',name:'systemManagement-supplier',component: Supplier ,meta: {title: '系统管理 · 供应商资料管理'} },
    {path: '/systemManagement/customer',name:'systemManagement-customer',component: Customer ,meta: {title: '系统管理 · 客户资料管理'} },
    {path: '/systemManagement/warehouse',name:'systemManagement-warehouse',component: Warehouse ,meta: {title: '系统管理 · 仓库管理'} },
    {path: '/systemManagement/product',name:'systemManagement-product',component: Product ,meta: {title: '系统管理 · 商品资料管理'} },
    {path: '/systemManagement/unitIndex',name:'systemManagement-unitIndex', component: UnitIndex,meta: {title: '系统管理 · 计量单位管理'}},

            { path: 'order', name: 'order', component: Order, meta: { title: '订单管理' } },
            { path: 'order/list', name: 'order-list', component: Order, meta: { title: '订单管理 · 订单列表' } },
            { path: 'order/return', name: 'order-return', component: OrderReturn, meta: { title: '订单管理 · 订单退货' } },

        ]
    },

    // 404 重定向
    {
        path: '/:pathMatch(.*)*',
        redirect: '/dashboard'
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 动态设置标题
// router.beforeEach((to, from, next) => {
//     if(to.path==='/login'){
//         next()
//     }else {
//         let token= sessionStorage.getItem("token")
//         if (token == null) {
//             next({
//                 path:'/login'
//             })
//         }else {
//             next()
//         }
//     }
// })

export default router